
import common from '../common';
// import MY_FUNC from '../../Component/test';

//大题题号换成汉字
function switchnum(num) {
  var unitPos = 0;
  var strIns = '', chnStr = '';
  var needZero = false;
  var han = /^[\u4e00-\u9fa5]+$/;
  if (han.test(num)) {
    chnStr = num//如果是汉字(直接返回不处理)
  }
  else {
    num = parseInt(num)
    var chnNumChar = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
    var chnUnitSection = ["", "万", "亿", "万亿", "亿亿"];
    var chnUnitChar = ["", "十", "百", "千"];
    function SectionToChinese(section) {
      var strIns = '', chnStr = '';
      var unitPos = 0;
      var zero = true;
      while (section > 0) {
        var v = section % 10;
        if (v === 0) {
          if (!zero) {
            zero = true;
            chnStr = chnNumChar[v] + chnStr;
          }
        } else {
          zero = false;
          strIns = chnNumChar[v];
          strIns += chnUnitChar[unitPos];
          chnStr = strIns + chnStr;
        }
        unitPos++;
        section = Math.floor(section / 10);
      }
      return chnStr;
    }

    if (num === 0) {
      return chnNumChar[0];
    }
    while (num > 0) {
      var section = num % 10000;
      if (needZero) {
        chnStr = chnNumChar[0] + chnStr;
      }
      strIns = SectionToChinese(section);
      strIns += (section !== 0) ? chnUnitSection[unitPos] : chnUnitSection[0];
      chnStr = strIns + chnStr;
      needZero = (section < 1000) && (section > 0);
      num = Math.floor(num / 10000);
      unitPos++;
    }
  }

  return chnStr;
}

//判断题目类型的函数
function testtype(typecode) {
  switch (typecode) {
    case "1ab96071-3741-40b7-983c-bba5a031fe0d":
      return "综合题"
      break;
    case "162104c2-ff76-41d1-9ed5-9fe1f1465597":
      return "填空题"
      break;
    case "c97bcad1-b425-4dd8-880b-bde06cf7b532":
      return "单选题"
      break;
    case "f9ea5d79-9e4f-44e3-9c70-6ea248323955":
      return "多选题"
      break;
  }
}

//解析选择题选项（suboption）
function suboption(suboption) {
  var strs = []
  //如果选择题选项字段内容符合规范
  if (suboption.indexOf('|||||') >= 0) {
    //首先用substring掐头去尾|||||
    //再利用|||||分割题目选项
    strs = suboption.substring(5, suboption.length - 5).split("|||||");
    var select = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];//选项字母（最多支持7个选项）
    var select_ = []
    for (var i = 0; i < strs.length; i++) {
      select_.push(select[i] + ' . ' + strs[i])
    }
    return select_
  }
  else {
    return strs
  }
}

// 添加上横线,去除p标签
function replaceStrBk(str) {
  while (str.indexOf('bk[1]bk') >= 0) {
    str = str.replace('bk[1]bk', '_________');
  }
  while (str.indexOf('bk[2]bk') >= 0) {
    str = str.replace('bk[2]bk', '_________');
  }
  while (str.indexOf('bk[3]bk') >= 0) {
    str = str.replace('bk[3]bk', '_________');
  }
  while (str.indexOf('<p>') >= 0) {
    str = str.replace('<p>', '');
  }
  while (str.indexOf('</p>') >= 0) {
    str = str.replace('</p>', '');
  }
  return str
}

//填空题的分类
function gapFilling(str) {

  function patch(re, s) {
    var n = (s.split(re)).length - 1;
    return n
  }
  //获取每一个填空的类型（bk[后面的数字）
  function patch_(re, s, num) {
    var array = s.split(re)
    var g = array[num]
    var array_ = g.split("")
    var gg = array_[0]
    return gg
  }
  var list = [];
  if (str.indexOf('bk[') >= 0) {
    //获取填空的类型，1是普通填空，2是画图，3是录音
    var num = patch('bk[', str);
    for (let i = 0; i <= num - 1; i++) {
      var type = patch_('bk[', str, i + 1)
      if (type == "1") {
        type = "填空题_键盘输入类型"
      } else if (type == "3") {
        type = "填空题_语言输入类型"
      } else if (type == "2") {
        type = "填空题_手绘输入类型"
      }
      list.push(type);
    }
  }
  // console.log(list);
  return list
}

function answerSheet(Jsondata, dati) {
  Jsondata.papercontent = unescape(Jsondata.papercontent);
  var timu = JSON.parse(Jsondata.papercontent);
  //console.log(JSON.parse(Jsondata.papercontent))
  for (var a = 0; a < timu.length; a++) {
    var BqNo = switchnum(timu[a].BqNo);//解析第几题
    timu[a].BqNo = "第" + BqNo + "题";//获取试题内容
    for (var i = 0; i < timu[a].qt.length; i++) {
      timu[a].qt[i].typecode = testtype(timu[a].qt[i].typecode);//判断题目类型的函数
      let type = timu[a].qt[i].typecode;
      if (type == "单选题" || type == "多选题") {
        timu[a].qt[i].suboption = suboption(timu[a].qt[i].suboption);
        //   //解析选择题选项（suboption）
      } else if (type == "填空题") {
        timu[a].qt[i].typecode = gapFilling(timu[a].qt[i].subcontent);
        timu[a].qt[i].subcontent = replaceStrBk(timu[a].qt[i].subcontent);
        //   // console.log(timu[a].qt[i].subcontent);
        //   // console.log(timu[0].qt[0].subcontent);
      } else if (type == "综合题") {
        for (var j = 0; j < timu[a].qt[i].csub.length; j++) {
          // timu[a].qt[i].csub[j].suboption
          let zonghetype = timu[a].qt[i].csub[j].typecode_dictionary_title
          if (zonghetype == "单选题" || zonghetype == "多选题") {
            timu[a].qt[i].csub[j].suboption = suboption(timu[a].qt[i].csub[j].suboption);
            //   //解析选择题选项（suboption）
          } else if (zonghetype == "填空题") {
            timu[a].qt[i].csub[j].typecode = gapFilling(timu[a].qt[i].csub[j].subcontent);
            timu[a].qt[i].csub[j].subcontent = replaceStrBk(timu[a].qt[i].csub[j].subcontent);
          }
        }
      }
      // console.log(timu[a].qt[i].subcontent);        
    }
  }
  dati(timu);
}
function answerSheetdz(Jsondata, dati) {
  Jsondata.papercontent = unescape(Jsondata.papercontent);
  var timu = Jsondata;
  timu.typecode = testtype(timu.typecode);//判断题目类型的函数
  let type = timu.typecode;
  if (type == "单选题" || type == "多选题") {
    timu.suboption = suboption(timu.suboption);
    //       //   //解析选择题选项（suboption）
  } else if (type == "填空题") {
    timu.typecode = gapFilling(timu.subcontent);
    timu.subcontent = replaceStrBk(timu.subcontent);
    //       //   // console.log(timu.subcontent);
    //       //   // console.log(timu[0].qt[0].subcontent);
  } else if (type == "综合题") {
    for (var j = 0; j < timu.csub.length; j++) {
      // timu.csub[j].suboption
      let zonghetype = timu.csub[j].typecode_dictionary_title
      if (zonghetype == "单选题" || zonghetype == "多选题") {
        timu.csub[j].suboption = suboption(timu.csub[j].suboption);
        //           //   //解析选择题选项（suboption）
      } else if (zonghetype == "填空题") {
        timu.csub[j].typecode = gapFilling(timu.csub[j].subcontent);
        timu.csub[j].subcontent = replaceStrBk(timu.csub[j].subcontent);
      }
    }
  }
  dati(timu);
}

function titleInformation(res, xinxi) {
  var blxiaoti = [];
  var datida = [];
  for (let a = 0; a < res.length; a++) {
    datida[a] = res[a].BqNo
    blxiaoti[a] = new Array(a);
    for (let c = 0; c < res[a].qt.length; c++) {
      blxiaoti[a][c] = c
    }
  }

  titleInformation.blxiaoti = blxiaoti;
  titleInformation.datida = datida;

  xinxi(titleInformation);

}


module.exports = {
  answerSheet: answerSheet,
  answerSheetdz: answerSheetdz,
  titleInformation: titleInformation,
  testtype: testtype,
}
